﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Http;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace common
{
    public class CurUser : ApiController
    {
        static string key = "Rkb4jvUy/ye7Cd7k98QQgQ==";
        public UserInfo get_()
        {
            FormsIdentity id = (FormsIdentity)User.Identity;
            UserInfo uo = CurUser.Decrypt(id.Ticket.UserData);
            return uo;
        }

        public static UserInfo get()
        {
            CurUser o = new CurUser();
            return o.get_();
        }

        public static string Encrypt(UserInfo user)
        {
            string str = user.username + ";"
                + user.useruid + ";"
                + user.code + ";"
                + user.name + ";"
                + user.role + ";"
                + user.account + ";"
                + user.userhostaddress;
            string estr = AESHelper.AESEncrypt(str, key);
            return estr;
        }

        public static UserInfo Decrypt(string ticket)
        {
            string str = AESHelper.AESDecrypt(ticket, key);
            string[] strs = str.Split(';');
            UserInfo o = new UserInfo
            {
                username = strs[0],
                useruid = strs[1],
                code = strs[2],
                name = strs[3],
                role = strs[4],
                account = strs[5],
                userhostaddress = strs[6]
            };
            return o;

        }
    }

    public class UserInfo
    {
        public string username { get; set; }
        public string useruid { get; set; }
        public string code { get; set; }
        public string name { get; set; }
        public string role { get; set; }
        public string account { get; set; }
        public string userhostaddress { get; set; }
    }
}